package indi.h.security.signature;

import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

import indi.h.security.Parameters;

public class SVFactoryConfigImp<T extends Enum<?>> implements SVFactoryConfig<T> {
	private T o;
	protected static Map<Enum<?>, Integer> defaultLength;
	protected static Map<Enum<?>, String> defaultSignature;
	protected static Map<Enum<?>, String> defaultEpsName;
	static {
		defaultLength = new HashMap<Enum<?>, Integer>();
		defaultSignature = new HashMap<Enum<?>, String>();
		defaultEpsName = new HashMap<Enum<?>, String>();

		defaultLength.put(SVType.RSA, 1024);
		defaultLength.put(SVType.ECDH, 192);// key must be
											// 192/239/256/224/384/521
		defaultLength.put(SVType.ECGOST3410_2012, 239);
		defaultLength.put(SVType.ECGOST3410, 1024);
		defaultLength.put(SVType.ECDHC, 192);// key must be
												// 192/239/256/224/384/521
		defaultLength.put(SVType.GOST3410, 1024);
		defaultLength.put(SVType.ECIES, 192);// key must be
												// 192/239/256/224/384/521
		defaultLength.put(SVType.DSA, 1024);
		defaultLength.put(SVType.ECDHWITHSHA1KDF, 192);// key must be
														// 192/239/256/224/384/521
		defaultLength.put(SVType.DSTU4145, 1024);
		defaultLength.put(SVType.EC, 571);// Key size must be at most 571 bits
		defaultLength.put(SVType.ECDSA, 192);// key must be
												// 192/239/256/224/384/521
		defaultLength.put(SVType.ECMQV, 256);// key must be
												// 192/239/256/224/384/521

		defaultSignature.put(SVType.RSA, "RSA");
		defaultSignature.put(SVType.ECDH, "ECDSA");
		defaultSignature.put(SVType.ECGOST3410_2012, "ECGOST3410-2012-512");
		defaultSignature.put(SVType.ECGOST3410, "ECGOST3410");
		defaultSignature.put(SVType.ECDHC, "ECDSA");
		defaultSignature.put(SVType.GOST3410, "GOST3410");
		defaultSignature.put(SVType.ECIES, "ECDSA");
		defaultSignature.put(SVType.DSA, "DSA");
		defaultSignature.put(SVType.ECDHWITHSHA1KDF, "ECDSA");
		defaultSignature.put(SVType.DSTU4145, "DSTU4145");
		defaultSignature.put(SVType.EC, "SHA512WITHPLAIN-ECDSA");
		defaultSignature.put(SVType.ECDSA, "ECDSA");
		defaultSignature.put(SVType.ECMQV, "ECDSA");

		defaultEpsName.put(SVType.ECGOST3410_2012, "GostR3410-2001-CryptoPro-A");// 参数来源,ECGOST3410NamedCurves.getNames()
		defaultEpsName.put(SVType.ECGOST3410, "GostR3410-2001-CryptoPro-C");// 参数来源,ECGOST3410NamedCurves.getNames()
		defaultEpsName.put(SVType.DSTU4145, "1.2.804.2.1.1.1.1.3.1.1.2.0");// 参数来源,DSTU4145NamedCurves.getOIDs()
	}

	@Override
	public void init(T o) {
		// TODO Auto-generated method stub
		this.o = o;
	}

	@Override
	public Parameters getParameters() {
		// TODO Auto-generated method stub
		SVParameter svp = new SVParameter();
		svp.setAlgorithm(o.toString().replaceAll("_", "-"));
		svp.setCode(Charset.forName("utf-8"));
		svp.setLength(defaultLength.get(o));
		svp.setSignAlgorithm(defaultSignature.get(o));
		return svp;
	}

	@Override
	public AbstractSV newAbstractSV() {
		// TODO Auto-generated method stub
		if (defaultEpsName.containsKey(o))
			return new ECGOST_SV(defaultEpsName.get(o));
		return new SignatureVerification();
	}

}
